import requests
import json

def send_dingtalk_message(webhook, message):
    # 构造消息体
    payload = {
        "msgtype": "text",  # 消息类型
        "text": {
            "content": message  # 消息内容
        }
    }

    # 将消息体转为 JSON 格式
    headers = {
        "Content-Type": "application/json"
    }

    # 发送 POST 请求到钉钉机器人 Webhook
    try:
        response = requests.post(webhook, data=json.dumps(payload), headers=headers)
        # 判断是否成功
        if response.status_code == 200:
            print("消息发送成功")
        else:
            print(f"发送失败，状态码：{response.status_code}")
    except Exception as e:
        print(f"请求失败: {e}")

# 示例：发送一条简单的文本消息
webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=66b2ce9743f9dc3e49e4e6cac141bff80854a1ba69fbcfff8e747770792649c6&sign=SEC2e2dfa8375e5c6acfa333fdbaac09082f21df19a5b95a213ccf601ce9608d398"  # 替换为你的 Webhook 地址
message = "你好，钉钉！这是通过 Python 发送的消息。"

send_dingtalk_message(webhook_url, message)
